<?php

namespace app\test\controller;

use think\Controller;
use PhpOffice\PhpWord\PhpWord;
use wxkxklmyt\Scws;
use QL\QueryList;
use GuzzleHttp\Client;

class Index extends Controller
{
    public function scws()
    {
        $text = $this->request->param('txt');
        $scws = new Scws();
        dump($scws->scws($text, 10));
    }

    public function word()
    {
        $phpWord = new PhpWord();
        $section = $phpWord->addSection();
        $fontStyle = [
            'name' => 'Microsoft Yahei UI',
            'size' => 20,
            'color' => '#ff6600',
            'bold' => true
        ];
        $textrun = $section->addTextRun();
        $textrun->addText('你好，这是生成的Word文档。 ', $fontStyle);
        $section->addLink('https://www.helloweba.net', '欢迎访问Helloweba', array('color' => '0000FF', 'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE));
        $section->addTextBreak();
        $section->addImage('https://zos.alipayobjects.com/rmsportal/UXamdIxYSkXfoVo.jpg', array('width' => 100, 'height' => 100));
        $header = $section->addHeader();
        $header->addText('Subsequent pages in Section 1 will Have this!');
        $section = $phpWord->addSection();
        $section->addText('新的一页.');
        $header = array('size' => 16, 'bold' => true);
        $rows = 10;
        $cols = 5;
        $section->addText('Basic table', $header);
        $table = $section->addTable();
        for ($r = 1; $r <= 8; $r++) {
            $table->addRow();
            for ($c = 1; $c <= 5; $c++) {
                $table->addCell(1750)->addText("Row {$r}, Cell {$c}");
            }
        }
        $file = 'test.docx';
        header("Content-Description: File Transfer");
        header('Content-Disposition: attachment; filename="' . $file . '"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Expires: 0');
        $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $xmlWriter->save("php://output");
    }

    public function queryList()
    {
        ini_set('max_execution_time',0);
        $rules = array(
            'title' =>
                array(
                    0 => '.title>a',
                    1 => 'text',
                ),
            'link' =>
                array(
                    0 => '.title>a',
                    1 => 'href',
                ),
        );
        $ql = new QueryList();
        $data = QueryList::get('https://www.ithome.com')->rules($rules)->range('')->queryData();
        //打印结果
//        echo '<pre>';
//        print_r($data);
        $page = [];
        foreach ($data as $url) {
            $rules = array(
                'html' =>
                    array(
                        0 => '#paragraph',
                        1 => 'html',
                    )
            );
            $page[] = QueryList::get($url['link'])->rules($rules)->query()->getData();
            // 释放资源，销毁内存占用
            $ql->destruct();
            break;
        }
        echo '<pre>';
        print_r($page);
    }

    public function code()
    {
        return $this->fetch();
    }
}
